package com.company.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import model.Libro;
import model.ListaUtile;
import model.Opcione;
import model.Padre;
import model.Persona;
import model.Tutor;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.SessionAware;
import org.omg.CORBA.PRIVATE_MEMBER;

import com.company.common.util.Utilitario;

@Action(value="envio-correo",
results={
	@Result(name="exito", location="/correo/enviar-correo.jsp"),
	@Result(name="error", location="/inicio.jsp")
} )
public class EnvioMailAction implements SessionAware{

	private Map<String, Object> session;
	
	public Map<String, Object> getSession() {
	     return session;
	}
	public void setSession(Map<String, Object> session) {
	     this.session = session;
	}

	public String execute() throws IOException{
		String result="exito";
		
		EntityManager em = Utilitario.getInstance().getEntityManager();

		Query q = em.createQuery("SELECT p FROM Padre p WHERE p.correo is not null");
		List<Padre> listadoPadre;
		
		try{
			listadoPadre = (List<Padre> ) q.getResultList();
		}catch(NoResultException e){
			result="error";
			listadoPadre = null;
		}
		StringBuilder correosPadres = new StringBuilder(); 
		for (Padre padre : listadoPadre) {
			correosPadres.append("").append(padre.getCorreo()).append(",");
		}
		
		final String username = "cgiranoc@gmail.com";
		final String password = "mad45oul7";
 
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587"); 
 
		Session sessionMail = Session.getInstance(props,
		  new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		  });
 
		try {
 
			Message message = new MimeMessage(sessionMail);
			message.setFrom(new InternetAddress("cgiranoc@gmail.com", "Colegio A"));
			message.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse(correosPadres.toString()));
			message.setSubject("Envio de correo desde Sistema");
			message.setText("Estimados Apoderados, Fueran tan amables de acercarse para coordinar la lista de utiles.");
 
			Transport.send(message);
 
			session.put("message", "Envio Satisfactorio");
		} catch (MessagingException e) {
			System.err.println("el error es: "+e.getMessage());
			result="error";
			session.put("message", "Error en el Envio");
			
		}
		
		
		
		return result;
	}

}

